import {axiosClient} from "@/utils/axiosClient.js";

/**
 * 用户登录
 * @param loginName 登录名
 * @param password 密码
 * @param errorCallback 失败时回调函数
 * @returns {Promise<axios.AxiosResponse<any>|undefined>}
 */
export const userLogin = (loginName, password, errorCallback) =>
    axiosClient().post(
        'oauth/login',
        {"loginName": loginName, "password": password},
        {},
        undefined,
        errorCallback)

/**
 * 通过用户信息换取code
 * @param clientId
 * @param redirectUri
 * @param scope
 * @param state
 * @param errorCallback
 * @returns {Promise<axios.AxiosResponse<any>|undefined>}
 */
export const authCodeByUser = (clientId, redirectUri, scope, state, errorCallback) =>
    axiosClient().post(
        '/api/oauth/authorize',
        {
            "client_id": clientId,
            "redirect_uri": redirectUri,
            "scope": scope,
            "state": state
        },
        {},
        undefined,
        errorCallback)

/**
 * 通过code换token
 * @param grantType
 * @param code
 * @param redirectUri
 * @param clientId
 * @param clientSecret
 * @param errorCallback
 * @returns {Promise<axios.AxiosResponse<any>|undefined>}
 */
export const authTokenByCode = (grantType, code, redirectUri, clientId, clientSecret, errorCallback) =>
    axiosClient().post(
        '/api/oauth/token',
        {
            "grant_type": grantType,
            "code": code,
            "redirect_uri": redirectUri,
            "client_id": clientId,
            "client_secret": clientSecret
        },
        {},
        undefined,
        errorCallback)
